global root_path "/Volumes/Workgroup/Lab/Lab-heimb/Montenovo/Volunteering Paper/Data"
global otheritem_input "$root_path/03_ready_for_TAXSIM/taxsim_input"
global otheritem_output "$root_path/04_TAXSIM_output"
cd "$root_path/03_ready_for_TAXSIM"


capture log close
log using taxsim35_2005, replace

use fam2005er-small.dta, clear

** Generate taxsim ID
gen taxsimid = _n //12004
gen individual_id=_n 

*** Tax year => year

g year = 2004

*** State code => state
** Converts state codes from GSA to SOI for injection into Taxsim.
** PSID state codes come from the documentation to the 1981 extract.
g state = 0
replace state = 1 if state_residence == 01 /* Alabama */
replace state = 2 if state_residence == 50 /* Alaska */
replace state = 3 if state_residence == 02 /* Arizona */
replace state = 4 if state_residence == 03 /* Arkansas */
replace state = 5 if state_residence == 04 /* California */
replace state = 6 if state_residence == 05 /* Colorado */
replace state = 7 if state_residence == 06 /* Connecticut */
replace state = 8 if state_residence == 07 /* Delaware */
replace state = 9 if state_residence == 8 /* DC */
replace state = 10 if state_residence == 09 /* Florida */
replace state = 11 if state_residence == 10 /* Georgia */
replace state = 12 if state_residence == 51 /* Hawaii */
replace state = 13 if state_residence == 11 /* Idaho */
replace state = 14 if state_residence == 12 /* Illinois */
replace state = 15 if state_residence == 13 /* Indiana */
replace state = 16 if state_residence == 14 /* Iowa */
replace state = 17 if state_residence == 15 /* Kansas */
replace state = 18 if state_residence == 16 /* Kentucky */
replace state = 19 if state_residence == 17 /* Louisiana */
replace state = 20 if state_residence == 18 /* Maine */
replace state = 21 if state_residence == 19 /* Maryland */
replace state = 22 if state_residence == 20 /* Massachusetts */
replace state = 23 if state_residence == 21 /* Michigan */
replace state = 24 if state_residence == 22 /* Minnesota */
replace state = 25 if state_residence == 23 /* Mississippi */
replace state = 26 if state_residence == 24 /* Missouri */
replace state = 27 if state_residence == 25 /* Montana */
replace state = 28 if state_residence == 26 /* Nebraska */
replace state = 29 if state_residence == 27 /* Nevada */
replace state = 30 if state_residence == 28 /* NewHampshire */
replace state = 31 if state_residence == 29 /* NewJersey */
replace state = 32 if state_residence == 30 /* NewMexico */
replace state = 33 if state_residence == 31 /* NewYork */
replace state = 34 if state_residence == 32 /* NorthCarolina */
replace state = 35 if state_residence == 33 /* NorthDakota */
replace state = 36 if state_residence == 34 /* Ohio */
replace state = 37 if state_residence == 35 /* Oklahoma */
replace state = 38 if state_residence == 36 /* Oregon */
replace state = 39 if state_residence == 37 /* Pennsylvania */
replace state = 40 if state_residence == 38 /* RhodeIsland */
replace state = 41 if state_residence == 39 /* SouthCarolina */
replace state = 42 if state_residence == 40 /* SouthDakota */
replace state = 43 if state_residence == 41 /* Tennessee */
replace state = 44 if state_residence == 42 /* Texas */
replace state = 45 if state_residence == 43 /* Utah */
replace state = 46 if state_residence == 44 /* Vermont */
replace state = 47 if state_residence == 45 /* Virginia */
replace state = 48 if state_residence == 46 /* Washington */
replace state = 49 if state_residence == 47 /* WestVirginia */
replace state = 50 if state_residence == 48 /* Wisconsin */
replace state = 51 if state_residence == 49 /* Wyoming */

// marital status is married if marital_status_head==1. if single, set marital_status_head==2 
replace marital_status_head=2 if marital_status_head!=1

*** Marital status => marital
// in PSID, marital_status_head=1 if married
// in Taxsim, marital=1 if single
* Making single if never married, widowed, divorced, separated, or other.
generate marital = .
replace marital=1 if marital_status_head==2
replace marital=2 if marital_status_head==1 
// rename to name wanted by taxsim
rename marital mstat

*** Dependents => dependents
g dependents = children
// rename to name wanted by taxsim
rename dependents depx

// For now, we assume all children are younger than 13
// Zwick wrote: Number of dependents under 17 (for tax credit). Need to match individual details file with tax unit file and compute correct age during tax year.
g dep13 = children
g dep17 = children
g dep18 = children

// ages of taxpayers
rename age_head page
rename age_wife sage
*assert age_wife == 0 if marital == 1, otherwise taxsim will return an error
replace sage=0 if mstat == 1

// Dividends
g dividends_taxable = dividend_inc_head_gen + dividend_inc_wife_gen
// rename to name wanted by taxsim
rename dividends_taxable dividends

* In 2005, labor and asset income for farms and gardening was combined.
g asset_income = rent_inc_head_gen + ///
                 farm_inc_head_gen + ///
                 unincorp_assetpt_inc_head_gen + ///
                 gardening_inc_head_gen + ///
                 alimony_inc_head_gen + ///
                 rent_inc_wife_gen + unincorp_assetpt_inc_wife_gen ///
				 
*** Labor income for taxpayer => salary_head
*** Labor income for spouse => salary_spouse
* PSID labor_inc_head_gen (ER40921) Includes:
*   -> wages and salaries (ER40903),
*   -> any separate reports of bonuses (ER40905), 
*   -> overtime (ER40907), 
*   -> tips (ER40909),
*   -> commissions (ER40911), 
*   -> professional practice or trade (ER40913),
*   -> market gardening (ER40915),
*   -> additional job income (ER40917),
*   -> and miscellaneous labor income (ER40919).       
* Methodology follows Butrica and Burkhauser (1997).
// labor income
g head_labor_income = taxable_inc_hh_gen - labor_inc_wife_gen - ///
                      dividend_inc_head_gen - dividend_inc_wife_gen - asset_income
g head_labor_income2 = labor_inc_head_gen
g salary_head = head_labor_income

// rename to name wanted by taxsim
rename salary_head pwages
rename labor_inc_wife_gen swages

// check that swage=0 for non joint returns
count if swages>0 & mstat==1
*drop if wages of spouse is greater than 0, otherwise taxsim will return an error
replace swages=0 if swages>0 & mstat==1

* If we want to use taxsim27, we have to separate interest received from the rest. Below I create the variable for interect received.
gen interest_received=interest_inc_head_gen + interest_inc_wife_gen
// rename to name wanted by taxsim
rename interest_received intrec

***Short term capital gains or losses. => short_capgains
//Assumption: short term capital gains equal to zero
g short_capgains = 0

***Long term capital gains or losses.
* Capital gains don't seem to be available except in supplementary files, which
* would probably be useful for verification. There may be a way to back this
* out of questions about assets. => long_capgains
//Assumption: long term capital gains equal to zero
g long_capgains = 0

// rename to name wanted by taxsim
rename short_capgains stcg
rename long_capgains ltcg

*** Other property income, including interest, rent, alimony, 
*** fellowships non-qualified dividends and other income or loss not otherwise 
*** enumerated here. Adjustments such as Keogh and IRA can be entered here as 
*** negative income.(+/-) => other_income
g alimony_paid = cond(alimony_given < 9999997, alimony_given, 0)
g other_income = asset_income - alimony_paid

// rename to name wanted by taxsim
rename other_income otherprop
rename alimony_paid nonprop

*** Taxable pension income. => pensions
/* Before summary vars.
g pension_head = cond(nonva_retiremt_head_amt < 999997, nonva_retiremt_head_amt, 0)
replace pension_head = 12*pension_head if nonva_retiremt_head_unit == 5
replace pension_head = 0 if nonva_retiremt_head_unit == 9
* Leaving wife out for now since her pension and annuity data is bundled. */
g pensions = nonva_retire_inc_head_gen + retire_annuities_inc_wife_gen

*** FIELD 12: Gross Social Security Income => social_security
g social_security = socsec_inc_head_gen + socsec_inc_wife_gen
// rename to name wanted by taxsim
rename social_security gssi

*** Unemployment compensation => unemployment
g unemployment = unemployment_inc_head_gen + unemployment_inc_wife_gen
// rename to name wanted by taxsim
rename unemployment ui

*** Other non-taxable transfer Income such as welfare, municipal 
*** bond interest, and child support that would affect eligibility for state 
*** property tax rebates but would not be taxable at the federal level. => transfers
g transfers = ssi_inc_head_gen + ssi_inc_wife_gen + ///
              vapension_inc_head_gen + ///
              workerscomp_inc_head_gen + workerscomp_inc_wife_gen + ///
              tanf_inc_head_gen + tanf_inc_wife_gen + ///
              otherwelfare_inc_head_gen + otherwelfare_inc_wife_gen + ///
              childsupport_inc_head_gen + childsupport_inc_wife_gen

*** Rent paid => rent
g rent = cond(rent_amount < 99997 & rent_amount_unit != 9, rent_amount, 0)
replace rent = 52*rent if rent_amount_unit == 3
replace rent = 26*rent if rent_amount_unit == 4
replace rent = 12*rent if rent_amount_unit == 5
// rename to name wanted by taxsim
rename rent rentpaid

*** Property taxes paid. => property_tax
g property_tax = cond(property_tax_annual < 99997, property_tax_annual, 0)
// rename to name wanted by taxsim
rename property_tax proptax

*** Itemized deductions other than mortgage,state income tax and 
*** local property tax. If you have medical expenses remember to only include 
*** the excess over 3%,5% or 7,5% of AGI (for years 60-82, 83-86 and 87+).
*** => itemized_total
* There are itemized fields and then we can also try to compute the value of 
* potential itemizations. Eventually, will try a fancier technique a la Bruce 
* (2000) or Ziliak and Kniesner (2005).
g item_char = cond(itemized_charitable < 999997, itemized_charitable, 0)
g item_med = cond(itemized_medical < 999997, itemized_medical, 0)
g itemized_total = cond(whether_itemized == 1, item_med, 0)
// rename to name wanted by taxsim
rename itemized_total otheritem

*** Child care expenses. => childcare
g childcare = cond(childcare_expense < 999997, childcare_expense, 0)

*** FIELD 20: Mortgage interest and other deductions not a preference for the
*** AMT, including charitable contributions, casulty losses in excess of 10% 
*** of AGI and medical expenses between 7.5% and 10% of AGI. => mortgage_interest
*** Ignore second mortgages. Assume all are fixed rates.
g mortgage_principle = cond(mortgage1_principle_remaining < 9999997, mortgage1_principle_remaining, 0)
g mortgage_rate_whole = cond(mortgage1_interest_rate_wholenum < 98, mortgage1_interest_rate_wholenum/100, 0)
g mortgage_rate_frac = cond(mortgage1_interest_rate_fraction < 998, mortgage1_interest_rate_fraction/100000, 0)
g mortgage_rate = mortgage_rate_whole + mortgage_rate_frac
*** First guess, just do 12 months of the same interest.
g mortgage_interest = mortgage_principle*mortgage_rate

g mortgage =  mortgage_interest + item_char 
drop mortgage_interest

** Clean up some messes.
replace page=40 if page==999
replace sage=40 if sage==999
replace pwages=0 if pwages<0


sum mstat depx page sage intrec pwages swages dividends otherprop nonprop gssi rentpaid proptax otheritem ui mortgage stcg ltcg


cd "$otheritem_input/2005"

save taxsim_input_otheritem.dta, replace

** Run taxsim 27

cd "$otheritem_output/2005"

taxsim35, full replace

use taxsim_out, clear

//v18 = Federal Taxable Income
//v36 = State Taxable Income

rename v18 federalTaxable
rename v36 stateTaxable

g fatr = fiitax/federalTaxable
replace fatr = 0 if fatr == .
g satr = siitax/stateTaxable
replace satr = 0 if satr == .
winsor fatr, gen(fatrW) p(.01)
replace fatr = fatrW
winsor satr, gen(satrW) p(.01)
replace satr = satrW

rename fiitax federalTax
rename siitax stateTax

rename taxsimid individual_id

rename v17 deductions_allowed

save taxsim27_2005_otheritem.dta, replace



